package com.htd.apm.configure;

import com.alibaba.dubbo.common.utils.StringUtils;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;


@Configuration
public class ElasticsearchConfig {

    @Value("${elasticsearch.host1}")
    private String esHost;
    @Value("${elasticsearch.host2:}")
    private String esHost2;
    @Value("${elasticsearch.port}")
    private int esPort;
    @Value("${elasticsearch.clustername}")
    private String esClusterName;
    @Value("${elasticsearch.clustername2}")
    private String esClusterName2;

    //写入告警数据ES
    @Bean
    public TransportClient client() throws Exception {

        Settings settings = Settings.builder()
                .put("cluster.name", esClusterName)
                .build();
        TransportClient transportClient = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName(esHost), esPort));
//        if (StringUtils.isNotEmpty(esHost2)) {
//            transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(esHost2), esPort));
//        }
        return transportClient;
    }
    
    //获取业务日志ES
    @Bean(name = "client4Bussiness")
    public TransportClient client1() throws Exception {

        Settings settings = Settings.builder()
                .put("cluster.name", esClusterName2)
                .build();
        TransportClient transportClient = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName(esHost2), esPort));
//        if (StringUtils.isNotEmpty(esHost2)) {
//            transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName(esHost2), esPort));
//        }
        return transportClient;
    }

}
